Apache Commons Collections লাইব্রেরি CollectionUtils ক্লাসটি বিভিন্ন ধরনের collection এর জন্য সহায়ক ইউটিলিটি মেথড প্রদান করে, যা ডেটা ম্যানিপুলেশন এবং প্রক্রিয়াকরণকে সহজ করে তোলে। এর মধ্যে দুটি গুরুত্বপূর্ণ মেথড হলো transform এবং forAllDo। এই মেথড দুটি কালেকশনের উপাদানগুলির উপর নির্দিষ্ট কাজ করতে ব্যবহার করা হয়, যেমন transformation এবং action অপারেশন।
CollectionUtils.transform
CollectionUtils.transform মেথডটি একটি collection এর উপাদানগুলির উপর একটি transformation প্রয়োগ করার জন্য ব্যবহৃত হয়। এটি একটি Function এর মাধ্যমে, প্রতিটি উপাদানকে রূপান্তর (transform) করে এবং ফলস্বরূপ একটি নতুন collection তৈরি করে।
Function হল একটি ইন্টারফেস যা একটি ইনপুট মানকে একটি আউটপুট মানে রূপান্তরিত করে। এটি একটি ফাংশনাল প্রোগ্রামিং কৌশল যা stream processing এর মতো কাজ করে।
Usage Example: CollectionUtils.transform
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import java.util.Arrays;
import java.util.List;
public class CollectionUtilsExample {
public static void main(String[] args) {
List<String> list = Arrays.asList("apple", "banana", "cherry");
// Define the transformer (convert each string to uppercase)
Transformer<String, String> transformer = new Transformer<String, String>() {
public String transform(String input) {
return input.toUpperCase();
}
};
// Apply transformation
List<String> transformedList = (List<String>) CollectionUtils.collect(list, transformer);
// Print the transformed list
System.out.println("Transformed List: " + transformedList);
}
}
Output:
Transformed List: [APPLE, BANANA, CHERRY]
ব্যাখ্যা:
CollectionUtils.collect()মেথডটি একটিTransformerব্যবহার করে list এর সমস্ত উপাদানকে রূপান্তরিত করেছে (এই ক্ষেত্রে, প্রতিটি স্ট্রিংকে uppercase এ রূপান্তরিত করা হয়েছে)।Transformerইন্টারফেসটি একটি function এর মতো কাজ করে, যা ইনপুট (একটি স্ট্রিং) গ্রহণ করে এবং একটি আউটপুট (uppercase স্ট্রিং) প্রদান করে।
CollectionUtils.forAllDo
CollectionUtils.forAllDo মেথডটি একটি action প্রয়োগ করে প্রতিটি উপাদানকে এক্সিকিউট করতে ব্যবহৃত হয়। এটি Closure ইন্টারফেস ব্যবহার করে, যার মাধ্যমে আমরা কালেকশনের সমস্ত উপাদান উপর একটি নির্দিষ্ট কার্যক্রম (action) চালাতে পারি।
Closure হল একটি ফাংশনাল প্রোগ্রামিং কৌশল, যা একটি কার্যক্রমকে একটি প্রক্রিয়ায় (action) রূপান্তরিত করে এবং প্রতিটি উপাদানের উপর সেই কার্যক্রম প্রয়োগ করে।
Usage Example: CollectionUtils.forAllDo
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Closure;
import java.util.Arrays;
import java.util.List;
public class CollectionUtilsExample {
public static void main(String[] args) {
List<String> list = Arrays.asList("apple", "banana", "cherry");
// Define the closure (print each element in the list)
Closure<String> printClosure = new Closure<String>() {
public void execute(String input) {
System.out.println(input);
}
};
// Apply the closure to all elements
CollectionUtils.forAllDo(list, printClosure);
}
}
Output:
apple
banana
cherry
ব্যাখ্যা:
CollectionUtils.forAllDo()মেথডটিClosureব্যবহার করে প্রতিটি উপাদানের উপর নির্দিষ্ট action (এই ক্ষেত্রে, প্রতিটি উপাদানকে প্রিন্ট করা) চালায়।Closureইন্টারফেসটি একটি action হিসাবে কাজ করে, যা একটি প্রক্রিয়া বা কার্যকলাপ (এই ক্ষেত্রে, স্ট্রিং আউটপুট) নির্দিষ্ট করে এবং সেটি সব উপাদানের উপর কার্যকর হয়।
Comparison: transform vs forAllDo
| Feature | transform | forAllDo |
|---|---|---|
| Purpose | Used to transform each element in a collection. | Used to apply an action to each element. |
| Output | Returns a new collection with transformed elements. | Performs an action on each element, no return value. |
| Usage | Used when you need to change the elements of a collection (e.g., map). | Used when you need to perform a side effect (e.g., print, modify external state). |
| Interface | Uses Transformer (a function). | Uses Closure (a procedure). |
| Example | Transforming a list of strings to uppercase. | Printing each element of a collection. |
When to Use transform vs forAllDo
- Use
transformwhen you want to change or transform the data in a collection. It creates a new collection with transformed elements. For example, when you need to apply a function (e.g., converting strings to uppercase or applying a mathematical function). - Use
forAllDowhen you need to apply an action (side effect) on each element in a collection. It does not return anything and is typically used when you want to perform operations like printing, updating external variables, or interacting with other systems.
CollectionUtils.transformএবংCollectionUtils.forAllDoহল দুটি অত্যন্ত শক্তিশালী এবং কার্যকরী মেথড যা Apache Commons Collections লাইব্রেরি থেকে পাওয়া যায়।transformমেথডটি কালেকশন উপাদানগুলির উপর ট্রান্সফরমেশন প্রয়োগ করে এবং একটি নতুন কালেকশন তৈরি করে, যেখানেforAllDoমেথডটি প্রতিটি উপাদানের উপর একটি action প্রয়োগ করে, যেমন প্রিন্ট করা বা কোন বৈশিষ্ট্য পরিবর্তন করা।- এগুলি Java কালেকশনের সঙ্গে কাজ করার সময় আরও নমনীয়তা এবং কার্যকারিতা প্রদান করে, যা আপনার প্রজেক্টের ডেটা ম্যানিপুলেশন প্রক্রিয়াকে সহজ এবং কার্যকরী করে তোলে।
Read more